#!/usr/bin/perl -w

use DBI;
use strict;

my $db = DBI->connect("dbi:SQLite:$ENV{HOME}/.local/share/powerdiary2.db", "", "",
{RaiseError => 1, AutoCommit => 1});

sub main
{
  my $date = shift || return(0);
  my ($day,$month,$year) = $date =~ /(\d+)\/(\d+)\/(\d+)/;

  my $sth = $db->prepare("SELECT content  FROM  entries where date=?");

  my $rv = $sth->execute($date);
  my $content=$sth->fetchrow_array();

  if( $content ){
    while ( $content =~ m/(.*)/g ) {
      my $tasktext = $1;
      if( $tasktext =~ m/\[doby\]/i ){

        $tasktext =~ s/\[doby\]//i;
        $tasktext =~ s/\"/\\\"/i;

        my $output =`export CLASSPATH=/opt/powerdiary2/lib/gdata-client-1.0.jar:/opt/powerdiary2/lib/gdata-core-1.0.jar:/opt/powerdiary2/lib/gdata-calendar-2.0.jar:/opt/powerdiary2/lib/gdata-calendar-meta-2.0.jar:/opt/powerdiary2/lib/google-collect-1.0-rc1.jar:/opt/powerdiary2/lib/; java MakeEntry \"$tasktext\" ` ;

       print $output;

      }
    }

  }
}

main(@ARGV);
